<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="98px">
      <el-form-item label="施工报备单号" prop="reportNum">
        <el-input
          v-model="queryParams.reportNum"
          placeholder="请输入施工报备"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="项目名称" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="撤场时间" prop="withdrawalTime">
        <el-date-picker clearable
          v-model="queryParams.withdrawalTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择撤场时间">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['check:withdrawalReport:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['check:withdrawalReport:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['check:withdrawalReport:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['check:withdrawalReport:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="withdrawalReportList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="撤场上报id" align="center" prop="withdrawalReportId" v-element-table-tooltip/>
      <el-table-column label="施工报备单号" align="center" prop="reportNum" />
      <el-table-column label="项目名称" align="center" prop="projectName"  width="200" v-element-table-tooltip/>
      <el-table-column label="占道报备单号" align="center" prop="jeevesNum" v-element-table-tooltip />
      <el-table-column label="撤场时间" align="center" prop="withdrawalTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.withdrawalTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="撤场类型" align="center" prop="endProject" :formatter="this.formatterEndProject" width="140" />
      <el-table-column label="备注内容" align="center" prop="remark" :show-overflow-tooltip='true' />
<!--      <el-table-column label="道路名称" align="center" prop="roadName" />-->
<!--      <el-table-column label="方向" align="center" prop="direction" />-->
      <el-table-column label="创建人" align="center" prop="createByName"  width="90"/>
      <el-table-column label="创建时间" align="center" prop="createTime" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="所属管辖大队" align="center" prop="deptName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['check:withdrawalReport:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['check:withdrawalReport:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row,scope.index)"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改撤场上报对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="施工报备单号" prop="reportInfoId">
          <el-select v-model="form.reportInfoId" filterable placeholder="请选施工报备单号" style="width: 100%"
                     @change="handleChange" :disabled="this.disabledSelectReportInfo">
            <el-option
              v-for="item in reportInfoOption"
              :key="item.reportInfoId"
              :label="item.reportNum"
              :value="item.reportInfoId">
              <span style="float: left">{{ item.reportNum }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.projectName }}</span>
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="占道报备单号" prop="jeevesReportId">
          <el-select v-model="form.jeevesReportId" filterable placeholder="请选择占道报备单号" style="width: 100%">
            <el-option
              v-for="item in jeevesReportOption"
              :key="item.jeevesReportId"
              :label="item.jeevesNum"
              :value="item.jeevesReportId">
              <span style="float: left">{{ item.jeevesNum }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.jeevesStartDate +'至'+ item.jeevesEndDate}}</span>
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="撤场时间" prop="withdrawalTime">
          <el-date-picker clearable
            v-model="form.withdrawalTime"
            type="date"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptions"
            placeholder="请选择撤场时间">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="撤场类型" prop="endProject">
          <el-select v-model="form.endProject" placeholder="请选择撤场类型">
            <el-option
              v-for="item in endProjectOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="撤场照片和视频" prop="withdrawalFileId">
          <file-upload  @input="getFileList" :fileLocationName="'withdrawalFile'" :value="this.withdrawalFileData" :fileType="['jpeg','png','jpg','video','mp4']" :limit="5" :fileSize="50"/>
        </el-form-item>

        <el-form-item label="备注内容" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="所属管辖大队" prop="deptName">
          <el-input v-model="form.deptName" placeholder="所属管辖大队" :disabled="true" />
        </el-form-item>
        <el-form-item label="联系人" prop="deptLeader">
          <el-input v-model="form.deptLeader" placeholder="联系人" :disabled="true" />
        </el-form-item>
        <el-form-item label="联系电话" prop="deptPhone">
          <el-input v-model="form.deptPhone" placeholder="联系电话" :disabled="true"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看撤场上报对话框 -->
    <el-dialog :title="title" :visible.sync="viewOpen" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="施工报备单号：">{{ form.reportNum }}</el-form-item>
        <el-form-item label="撤场时间：">{{ parseTime(form.withdrawalTime,'{y}-{m}-{d}') }}</el-form-item>
        <el-form-item label="撤场类型：">{{this.formatterEndProject(null,null,form.endProject) }}</el-form-item>

        <el-form-item label="撤场照片和视频：" prop="withdrawalFileId">
          <image-preview :value="this.withdrawalFileData" fileId='withdrawalFileId'/>
        </el-form-item>
        <el-form-item label="备注内容：" prop="remark">{{form.remark}}</el-form-item>
<!--        <el-form-item label="经纬度：" prop="longitude">{{form.longitude + ',' + form.latitude}}</el-form-item>-->
        <el-form-item label="道路名称：" prop="roadName">{{form.roadName}}</el-form-item>
        <el-form-item label="方向：" prop="direction">{{form.direction}}</el-form-item>
        <el-form-item label="所属管辖大队：" prop="deptName">{{form.deptName}}</el-form-item>
        <el-form-item label="联系人：" prop="deptLeader">{{form.deptLeader}}</el-form-item>
        <el-form-item label="联系电话：" prop="deptPhone">{{form.deptPhone}}</el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="viewOpen = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { selectNotWithdrawnList } from "@/api/check/jeevesReport";
import { listWithdrawalReport, getWithdrawalReport, delWithdrawalReport, addWithdrawalReport, updateWithdrawalReport } from "@/api/check/withdrawalReport";
import { deptUserTreeSelect } from "@/api/system/user";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { selectListreportInfo} from "@/api/check/reportInfo";
export default {
  name: "WithdrawalReport",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 撤场上报表格数据
      withdrawalReportList: [],

      //  禁用施工报备下拉列表选项 默认开启
      disabledSelectReportInfo : false,

      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        withdrawalTime: null,
        longitude: null,
        latitude: null,
        roadName: null,
        direction: null,
        withdrawalReportId: null,
        reportInfoId: null
      },
      // 表单参数
      form: {},
      //  组织部门
      deptOptions: undefined,
      //  报备信息option
      reportInfoOption:[],
      //  占道报备option
      jeevesReportOption:[],
      endProjectOptions:[{
        value: 0,
        label: '阶段性施工结束'
      },
        {
          value: 1,
          label: '整个项目施工结束'
        }],

      //  是否显示详情弹出层
      viewOpen: false,
      // 表单校验
      rules: {
        reportInfoId: [
          { required: true, message: "施工报备id不能为空", trigger: "blur" }
        ],
        jeevesReportId: [
          { required: true, message: "占道报备单号不能为空", trigger: "blur" }
        ],
        endProject: [
          { required: true, message: "撤场类型不能为空", trigger: "blur" }
        ],
        withdrawalTime: [
          { required: true, message: "撤场时间不能为空", trigger: "blur" }
        ],
        delFlag: [
          { required: true, message: "删除标志不能为空", trigger: "blur" }
        ],
        deptId: [
          { required: true, message: "所属管辖大队不能为空", trigger: "blur" }
        ],
      },
      pickerOptions: {
        disabledDate: (time) => {
          let nowData = new Date()
          nowData = new Date(nowData.setDate(nowData.getDate() - 1))
          return time < nowData
        }
      },
      //撤场现场 附件
      withdrawalFileData:[]
    };
  },
  created() {
    this.queryParams.withdrawalReportId = this.$route.query && this.$route.query.businessId;
    this.queryParams.reportInfoId = this.$route.query && this.$route.query.reportInfoId;
    this.getList();
  },
  methods: {
    /** 查询撤场上报列表 */
    getList() {
      this.loading = true;
      listWithdrawalReport(this.queryParams).then(response => {
        this.withdrawalReportList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        withdrawalReportId: null,
        reportInfoId: null,
        withdrawalTime: null,
        withdrawalFileId: null,
        remark: null,
        longitude: null,
        latitude: null,
        roadName: null,
        direction: null,
        delFlag: null,
        deptId: null,
        reviewedBy: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.withdrawalReportId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加撤场上报";
      this.getReportInfoList();
      //this.getDeptTree();
      this.disabledSelectReportInfo = false;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const withdrawalReportId = row.withdrawalReportId || this.ids
      getWithdrawalReport(withdrawalReportId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改撤场上报";
        //  撤场图片/视频
        this.withdrawalFileData = response.data.withdrawalFile;
        this.disabledSelectReportInfo = true;
        this.getReportInfoList();
        this.getJeevesNotWithdrawnList(response.data.reportInfoId);
      });
    },

    /** 详情按钮操作 */
    handleView(row) {
      this.reset();
      const withdrawalReportId = row.withdrawalReportId || this.ids
      getWithdrawalReport(withdrawalReportId).then(response => {
        this.form = response.data;
        this.viewOpen = true;
        this.title = "查看撤场上报";
        //  撤场图片/视频
        this.withdrawalFileData = response.data.withdrawalFile;
        this.getReportInfoList();
        //this.getDeptTree();
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.withdrawalReportId != null) {
            updateWithdrawalReport(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWithdrawalReport(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const withdrawalReportIds = row.withdrawalReportId || this.ids;
      this.$modal.confirm('是否确认删除撤场上报编号为"' + withdrawalReportIds + '"的数据项？').then(function() {
        return delWithdrawalReport(withdrawalReportIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('check/withdrawalReport/export', {
        ...this.queryParams
      }, `withdrawalReport_${new Date().getTime()}.xlsx`)
    },

    /** 查询施工报备信息列表 */
    getReportInfoList() {
      this.loading = true;
      selectListreportInfo({}).then(response => {
        this.reportInfoOption = response.data;
        this.loading = false;
      });
    },

    /** 查询部门下拉树结构 */
    // getDeptTree() {
    //   deptUserTreeSelect().then(response => {
    //     this.deptOptions = response.data;
    //   });
    // },

    //获取附件列表
    getFileList(fileLocationName,fileIds,deleteFileId){
      this.form[fileLocationName] = fileIds;
    },

    /** 施工报备单号选项 change事件 */
    handleChange(val){
      //选中的数据和options进行匹配
      var obj={}
      obj= this.reportInfoOption.find(function(i){
        return i.reportInfoId ===val
      });
      this.form.deptId = obj.deptId;
      this.form.deptName = obj.deptName;
      this.form.deptLeader = obj.deptLeader;
      this.form.deptPhone = obj.deptPhone;
      // 清空撤场上报id
      this.$set(this.form, 'jeevesReportId', '');
      this.getJeevesNotWithdrawnList(obj.reportInfoId);
    },

    /** 查询占道报备未撤场单子 */
    getJeevesNotWithdrawnList(reportInfoId) {
      this.loading = true;
      selectNotWithdrawnList({reportInfoId:reportInfoId}).then(response => {
        this.jeevesReportOption = response.data;
        this.loading = false;
      });
    },

    // 格式化撤场类型
    formatterEndProject(row, column, cellValue, index) {
      //  cellValue是当前单元格的值
      const obj = this.endProjectOptions.find(item => item.value === cellValue)
      return obj ? obj.label : '未知'
    },

  }
};
</script>
